<?php
    // Do not cache this page.
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header("Cache-Control: no-cache");
    header("Pragma: no-cache");

    include 'lib/chartConfig.php';

    if(isset ($_POST["action"]) && $_POST["action"] == "update")
    {
        //Check if actual template file, not a hack like../dbConn.php. :(
        //Suppress file read warnings, placeholders will not exist and the error
        //will be displayed.
        $commitTemplate = true;
        try
        {
            $myFile = 'lib/ECOTree/'.$_POST["templateFile"];
            $fh = @fopen($myFile, 'r');
            $template = @fread($fh, filesize($myFile));
            @fclose($fh);

            if(!strpos($template, "<!--PLACEHOLDER:TITLE-->") ||
               !strpos($template, "/*PLACEHOLDER:NODES*/") ||
               !strpos($template, "/*PLACEHOLDER:INDEXES*/"))
            {
                throw new Exception('Missing Placeholders');
            }
        }
        catch (Exception $ex)
        {
            echo 'Template file or location is invalid. Missing placeholders.<br />';
            $commitTemplate = false;
        }

        $newConfig = new chartConfig();
        $newConfig->root = $_POST["root"];
        $newConfig->depth = $_POST["depth"];
        $newConfig->title = $_POST["title"];
        ($commitTemplate) ? $newConfig->templateFile = $_POST["templateFile"] : $newConfig->templateFile = 'ChartTemplate.html';
        $newConfig->showName = (bool)$_POST["showName"];
        (!strpos($_POST["nameFormat"], '%s')) ? $newConfig->nameFormat = '%s' : $newConfig->nameFormat = $_POST["nameFormat"];
        $newConfig->showDiv = (bool)$_POST["showDiv"];
        (!strpos($_POST["divFormat"], '%s')) ? $newConfig->divFormat = '%s' : $newConfig->divFormat = $_POST["divFormat"];
        $newConfig->showTitle = (bool)$_POST["showTitle"];
        (!strpos($_POST["titleFormat"], '%s')) ? $newConfig->titleFormat = '%s' : $newConfig->titleFormat = $_POST["titleFormat"];
        
        commitConfig($newConfig);
        echo "Configuration updated.";
    }
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Set Chart Defaults</title>
        <script type="text/javascript" src="lib/jquery/jquery-1.4.2.min.js"></script>
        <script type="text/javascript">
            function hideFormat(question, format)
            {
                if($('#'+question).is(':checked')) $('#'+format).fadeIn('slow');
                else $('#'+format).fadeOut('slow');
            }
        </script>
        <style type="text/css" >
            body {
                font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
            }
            p {
                font-size: x-small;
            }

        </style>
    </head>
    <body>
        <?php
            $currentConfig = openConfig();
            //var_dump($currentConfig);exit;
        ?>
        <form method="post" enctype="multipart/form-data">
            <!-- Need to add some instructions for the user and JavaScript Validation-->
            <!-- Add option: Collapsed or expanded by default -->
            <h3>Chart Setup</h3>
            <p style="width:24em;">
                <em>Root:</em> Employee ID of top node (Boss).<br /><br />
                <em>Depth:</em> # of levels to display.<br /><br />
                <em>Template File:</em> This may be useful if you would like to customize multiple chart templates.
            </p>

            <input type="hidden" name="action" value="update">
            Root:<br />
            <input name="root" type="text" id="root" size="50" value="<?php echo $currentConfig->root ?>"><br />
            Depth:<br />
            <input name="depth" type="text" id="depth" size="50" value="<?php echo $currentConfig->depth ?>"><br />
            Title:<br />
            <input name="title" type="text" id="title" size="50" value="<?php echo $currentConfig->title ?>"><br />
            Template File:<br />
            <input name="templateFile" type="text" id="templateFile" size="50" value="<?php echo $currentConfig->templateFile ?>"><br /><br /><br />

            <h4>Node Contents</h4>
            <p style="width:24em;">
                You may format the node contents with html such as style, bold, etc. The %s is placeholder for the data and is required.
            </p>

            <input name="showName" type="checkbox" id="showName" value="true" <?php if($currentConfig->showName) echo "checked"; ?> onClick="hideFormat('showName', 'nameFormat')">Show Name<br />
            <div id="nameFormat" style="display:<?php echo (!$currentConfig->showName ? "none" : "inline"); ?>">Format:<br />
            <input name="nameFormat" type="text" size="50" value="<?php echo $currentConfig->nameFormat ?>"><br /></div><br />

            <input name="showDiv" type="checkbox" id="showDiv" value="true" <?php if($currentConfig->showDiv) echo "checked"; ?> onClick="hideFormat('showDiv', 'divFormat')">Show Div/Department<br />
            <div id="divFormat" style="display:<?php echo (!$currentConfig->showDiv ? "none" : "inline"); ?>">Format:<br />
            <input name="divFormat" type="text" size="50" value="<?php echo $currentConfig->divFormat ?>"><br /></div><br />

            <input name="showTitle" type="checkbox" id="showTitle" value="true" <?php if($currentConfig->showTitle) echo "checked"; ?> onClick="hideFormat('showTitle', 'titleFormat')">Show Title<br />
            <div id="titleFormat" style="display:<?php echo (!$currentConfig->showTitle ? "none" : "inline"); ?>">Format:<br />
            <input name="titleFormat" type="text" size="50" value="<?php echo $currentConfig->titleFormat ?>"><br /></div><br />
            
            <br /><input name="update" type="submit" id="upload" value=" Submit ">

            <h3>Disconnected Nodes</h3>
            <p style="width:24em;">
                If a supervisor is terminated the nodes connected to theirs will not display on the charts.<br />
                <a href="index.php?report=disconnected">View Disconnected Nodes</a>
            </p>
        </form>
    </body>
</html>
